package io.github.kuafucv.zero.log.core.http;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class HttpBuilder {

    private static String url;
    private static final Charset charset = StandardCharsets.UTF_8;;
    private static int httpReadTimeoutInMillis = 1000;
    private static int httpConnectionTimeoutInMillis = 1000;

    public static HttpURLConnection build() throws IOException {
        URL endpoint = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) endpoint.openConnection();
        conn.setReadTimeout(httpReadTimeoutInMillis);
        conn.setConnectTimeout(httpConnectionTimeoutInMillis);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Content-Type", "application/json" + "; charset=" + charset.name());
        conn.setRequestMethod("POST");
        return conn;
    }

    public static void setUrl(String url) {
        HttpBuilder.url = url;
    }

    public static void setHttpReadTimeoutInMillis(int httpReadTimeoutInMillis) {
        HttpBuilder.httpReadTimeoutInMillis = httpReadTimeoutInMillis;
    }

    public static void setHttpConnectionTimeoutInMillis(int httpConnectionTimeoutInMillis) {
        HttpBuilder.httpConnectionTimeoutInMillis = httpConnectionTimeoutInMillis;
    }
}
